//
//  PoeCell.m
//  FuncFanc
//
//  Created by hubing on 2018/4/12.
//  Copyright © 2018年 huhu. All rights reserved.
//

#import "PoeCell.h"
@interface PoeCell ()
{
    UILabel *_tip;
    UILabel *_authorDetail;
    UILabel *_verse;
    UILabel *_title;
    UIButton *_author;
    UIView *_backView;
}


@end


@implementation PoeCell

-(instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}

-(void)configPoeCellWithData:(Poem*)poe{
    self.backgroundColor = RGBOF(0xf1f1f1);

    if (!_title) {
        _title = [[UILabel alloc]init];
    }
    [self addSubview:_title];
    _title.text = [NSString stringWithFormat:@"%@",poe.title];
    [_title setSize:[_title.text textSizeWithFont:_title.font maxW:ScreenWidth-60]];
    _title.textAlignment = NSTextAlignmentCenter;
    _title.numberOfLines = 0;
    
   
    [_title layoutParentHorizontalCenter];
    [_title alignParentTopWithMargin:30];
    
   
    if (!_author) {
      _author = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    [_author setTitle:poe.authorName forState:UIControlStateNormal];
    [_author setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self addSubview:_author];
    [_author addTarget:self action:@selector(displayAuthorDetail) forControlEvents:UIControlEventTouchUpInside];
    [_author sizeToFit];
    [_author layoutBelow:_title margin:10];
    [_author alignParentRightWithMargin:100];
    
    if (_backView) {
        [_backView removeFromSuperview];
    }
    _backView = [[UIView alloc]init];
    _backView.backgroundColor = [UIColor clearColor];
    [self addSubview:_backView];
    [_backView alignParentLeft];
    [_backView layoutBelow:_author margin:-20];
    NSArray *paragraphs = poe.paragraphs;
  //  CGFloat margin = 30;
    CGFloat height = 0;
     [_backView setSize:CGSizeMake(ScreenWidth, 30)];
    for (int i = 0 ; i < paragraphs.count; i ++) {
         _verse = [[UILabel alloc]init];
//        _verse.backgroundColor = [UIColor greenColor];
        _verse.text = [NSString stringWithFormat:@"%@",paragraphs[i]];
        _verse.font = [UIFont fontWithName:@"Courier" size:16];
        [_verse sizeToFit];
        [_backView addSubview:_verse];
        height += CGRectGetHeight(_verse.frame)+30;
        [_verse setSize:[_verse.text textSizeWithFont:[UIFont fontWithName:@"Courier" size:20] maxW:ScreenWidth-80]];
        _verse.textAlignment = NSTextAlignmentCenter;
        _verse.numberOfLines = 0;
        [_verse layoutParentHorizontalCenter];
        [_verse alignParentTopWithMargin:height+5];
    }
    [_backView setSize:CGSizeMake(ScreenWidth, height+30)];
   
//    if (!_tip) {
//        _tip = [[UILabel alloc]init];
//    }
//    _tip.text = @"作者生平";
//    [self addSubview:_tip];
//
//    [_tip sizeToFit];
//    _tip.textAlignment = NSTextAlignmentLeft;
//    [_tip layoutBelow:_backView margin:30];
//    [_tip alignParentLeftWithMargin:40];
    
    
    
    _tip.hidden = YES;
    _authorDetail.hidden = YES;
    [self setHeight:CGRectGetMaxY(_backView.frame) + CGRectGetHeight(_backView.frame)];
}

-(void)displayAuthorDetail{
    if (!_tip) {
        _tip = [[UILabel alloc]init];
    }
    _tip.text = @"作者生平";
    [self addSubview:_tip];
    
    [_tip sizeToFit];
    _tip.textAlignment = NSTextAlignmentLeft;
    [_tip layoutBelow:_backView margin:30];
    [_tip alignParentLeftWithMargin:40];
    
    if (!_authorDetail) {
        _authorDetail = [[UILabel alloc]init];
    }
    Author *author = [[Author alloc]initWithName:_author.titleLabel.text];
    
    _authorDetail.text = [NSString stringWithFormat:@"_ _ _ _%@",author.songPoetDesc?author.songPoetDesc:author.tangPoetDesc];
    _authorDetail.font = [UIFont systemFontOfSize:17];
    [self addSubview:_authorDetail];
    
    [_authorDetail setSize:[_authorDetail.text textSizeWithFont:_authorDetail.font maxW:ScreenWidth-60]];
    _authorDetail.textAlignment = NSTextAlignmentLeft;
    _authorDetail.numberOfLines = 0;
    
  
    [_authorDetail layoutBelow:_tip margin:20];
    [_authorDetail alignParentLeftWithMargin:30];
    _tip.hidden = NO;
    _authorDetail.hidden = NO;
   [self setHeight:CGRectGetMaxY(_authorDetail.frame)+CGRectGetHeight(_authorDetail.frame)];
}




@end
